/* parheap.h - Data structures for the parallel heap */

#include <pthread.h>

#define MAX_INSERTS        2
#define MAX_CONNECTIONS    3
#define MAX_MESSAGES       50
#define MAX_SERVICE_TIME   15
#define NO_OF_GATES        5
#define MAX_LPS            10000
#define MAX_SHORTEST       4
#define TRUE               1
#define FALSE              0
#define _GNU_SOURCE

typedef struct
{
   int insert_flag;        /* if not -1, points to the node at which        */
                           /* insert-update process  is to be carried out   */
   int insert_level;       /* Level no. of insert items                     */
   int insert_offset;      /* Offset of insert items                        */
   int insert_no_of_items; /* No. of items to be inserted                   */
} INSERT_TYPE;

typedef struct
{
   int delete_flag;         /* if not -1, points to the node at which        */
                            /* delete-update process is to be carried out    */
   INSERT_TYPE Inserts[MAX_INSERTS];
} LevelFlagType;

typedef struct
{
   int item ;             /* node priority i.e. time-stamp of message */
/*
   int from;
   int to;
   int status ;
   int id;
*/
} PARHEAP, MESSAGE_TYPE;

typedef struct
{
   int NoOfMessages;                /* No. of messages */
   MESSAGE_TYPE **messages;
   int ConnectedLP;
} OUT_CHANNEL;

typedef struct
{
   int fromLP;    /* No. of LP from which this channel originated */
   int OutIndex;  /* Index of this channel in "out_channels" of that LP */
} IN_CHANNEL;

typedef struct
{
   int    service_time;         /* Service time of LP */
   int    elem_type;            /* Type of circuit element (GATE here) */
   int    status;               /* Current output of LP (0 or 1) ?? */
   double local_clock;          /* Local clock */
   int    in_channels_num;      /* No of input channels */
   int    out_channels_num;     /* No of output channels */
   IN_CHANNEL in_channels[MAX_CONNECTIONS];   /* Array of input channels */
   OUT_CHANNEL out_channels[MAX_CONNECTIONS];  /* Array of output channels */
} LP_TYPE;

typedef struct
{
  int LP[MAX_SHORTEST];
  int distance[MAX_SHORTEST];
} SHORTEST_DIST;


/*-----------Thread specific Variables/identifiers------------*/
typedef struct /* Arguments wrapped inside one structure to use when passing arguments */
{
  int NoOfInsertDeleteCycles;
  int NoOfProcesses;
  int RandomK;
  int ToBeDeleted;
  int ToBeInserted;
  int taskid;
} THREAD_DATA;

typedef struct /* Conditional wait and parameters for Prefetching thread */
{
    volatile int temp[2];
    pthread_cond_t cond;
    pthread_mutex_t mutex;
    volatile int spin;
    int tid;
} Prefetch_data;

#define OK_SPIN  1
#define NO_SPIN -1


typedef struct
{
    PARHEAP***          heap;
    PARHEAP*            deleted_items;
    PARHEAP*            inserted_items;
    size_t              r;
    size_t              n_worker;
    size_t              n_maintenaince;
    pthread_barrier_t*  all_barrier;
    pthread_barrier_t*  worker_barrier;
    pthread_barrier_t*  worker_and_p0_barrier;
    pthread_barrier_t*  maintenance_and_p0_barrier;
} PPQ;

